// components/iwant/index.js
import {Pk} from "../../model/pk"
import {promisic} from "../../utils/util"
import {Apply} from "../../model/apply"
import Toast from "../../miniprogram_npm/@vant/weapp/toast/toast";

Component({
  /**
   * 组件的属性列表
   */
  properties: {
    info: Object,
    punch: Object
  },

  /**
   * 组件的初始数据
   */
  data: {

  },

  /**
   * 组件的方法列表
   */
  methods: {
    async hasAuthUserInfo() {
      const setting = await promisic(wx.getSetting)()
      const userInfo = setting.authSetting['scope.userInfo']
      return !!userInfo
    },
    async judgeApply() {
      wx.showLoading({
        mask: true
      })
      const showLogin = !await this.hasAuthUserInfo()
      if (!showLogin) {
        // const apply = await Apply.getApplyInfo()
        const apply = wx.getStorageSync('apply')
        if (apply) {
          wx.hideLoading()
          return {
            tip: '',
            canUse: true
          }
        }else {
          wx.hideLoading()
          return {
            tip: '请先报名',
            canUse: false
          }
        }
      }else {
        wx.hideLoading()
        return {
          tip: '请先登录',
          canUse: false
        }
      }
    },
    async onStudyTap() {
      const app = await this.judgeApply()
      if (app.canUse) {
        wx.navigateTo({
          url: '/pages/study/study'
        })
      }else {
        wx.showToast({
          icon: "none",
          title: app.tip,
          duration: 3000
        })
      }
    },
    async onWalkTap() {
      const app = await this.judgeApply()
      if (app.canUse) {
        wx.navigateTo({
          url: '/pages/intangible/index'
        })
      }else {
        wx.showToast({
          icon: "none",
          title: app.tip,
          duration: 3000
        })
      }
    },
    async onPkTap() {
      // wx.showToast({
      //   title: '暂未开放, 敬请期待',
      //   icon: 'none',
      //   duration: 3000
      // })
      const app = await this.judgeApply()
      if (app.canUse) {
        wx.showLoading({
          title: '创建PK'
        })
        const pk = await Pk.createPk(8)
        if (pk) {
          wx.hideLoading()
          wx.navigateTo({
            url: `/pages/pk/pk?pkid=${pk.id}&source=left`
          })
        }
      }else {
        wx.showToast({
          icon: "none",
          title: app.tip,
          duration: 3000
        })
      }
    },
    async onAnswerTap() {
      // wx.showToast({
      //   title: '暂未开放, 敬请期待',
      //   icon: 'none',
      //   duration: 3000
      // })
      const app = await this.judgeApply()
      if (app.canUse) {
        wx.navigateTo({
          url: '/pages/answer/answer'
        })
      }else {
        wx.showToast({
          icon: "none",
          title: app.tip,
          duration: 3000
        })
      }
    }
  }
})
